package action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import bean.DB;
import util.DigestUtil;

public class AdminLoginAction extends ActionSupport {
	private String username;
	private String password;
	private String feedback;
	private String admin_id;
	

	Connection con=null;
	PreparedStatement pst=null;
	ResultSet rs=null;
	
	PreparedStatement pst1=null;
	ResultSet rs1=null;
	
	

	public String adminlogin() throws SQLException{
		feedback="";
		DB db=new DB();
		con=db.getCon();
		String sql1="select power from teachertable where power=?";
		pst1=con.prepareStatement(sql1);
		pst1.setInt(1, 1);
		rs1=pst1.executeQuery();
		if(rs1.next()) {
			String sql="select * from teachertable where teachername=? and teacherpass=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, username);
			//pst.setString(2, password);
			pst.setString(2, DigestUtil.md5(password));
			rs=pst.executeQuery();
			//System.out.println(username+password);
			//System.out.println(sql);
			//System.out.println(rs.next());
			if(rs.next()){
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				session.setAttribute("teacherid", rs.getString("teacherid"));
				session.setAttribute("teachername", username);
				feedback="loginsuccess";
				//System.out.println(feedback);
				//admin_id=rs.getString("admin_id");
				rs.close();
				pst.close();
				con.close();
				rs1.close();
				pst1.close();
				return SUCCESS;
			}else{
				feedback="loginflase";
				return ERROR;
			}
			
		}else {
			String sql="select * from admin where admin_username=? and admin_password=?";
			pst=con.prepareStatement(sql);
			pst.setString(1, username);
			pst.setString(2, password);	
			//pst.setString(2, DigestUtil.md5(password));
			rs=pst.executeQuery();
			//System.out.println(username+password);
			//System.out.println(sql);
			//System.out.println(rs.next());
			if(rs.next()){
				HttpServletRequest request = ServletActionContext.getRequest();
				HttpSession session = request.getSession();
				session.setAttribute("admin_id", rs.getString("admin_id"));
				session.setAttribute("admin_username", username);
				feedback="loginsuccess";
				//System.out.println(feedback);
				//admin_id=rs.getString("admin_id");
				rs.close();
				pst.close();
				con.close();
				rs1.close();
				pst1.close();
				return SUCCESS;
			}else{
				feedback="loginflase";
				rs.close();
				pst.close();
				con.close();
				rs1.close();
				pst1.close();
				return ERROR;
			}
			
			
		}
		
		
		
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getFeedback() {
		return feedback;
	}

	public void setFeedback(String feedback) {
		this.feedback = feedback;
	}


	public String getAdmin_id() {
		return admin_id;
	}

	public void setAdmin_id(String admin_id) {
		this.admin_id = admin_id;
	}


}
